<?php


namespace app\index\controller;


use app\common\Base;
use app\index\model\Business;
use app\index\model\Carts;
use app\index\model\Commodity;
use app\index\model\Coupon;
use app\index\model\CouponReceive;
use think\Exception;
use think\Request;

class ShoppingCart extends  Base
{

    //列出购物车数据(成功)
    /**
     * @param Request $request
     * @param Carts $carts
     * @param Commodity $commodity
     * @param Coupon $coupon
     * @param CouponReceive $couponReceive
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selCart(Request $request, Carts $carts,Commodity $commodity){
        // TODO 将获取缓存信息放在中间件里，请加上try catch
        $arr = $request->cacheInfo;
        $uId = $arr['id'];//获取用户id

        try{
            $businessName = $carts->selCartName($uId);

            //遍历多维数组
            $arraya = array();
            $arr = array();
            foreach($businessName as $key => $value){
                array_push($arraya,$businessName[$key]['businessName']);
            }
            //清除重复商家名
            $arraya = array_unique($arraya);
            foreach ($arraya as $k=>$v){
                array_push($arr ,$v);
            }

            $array  =array();
            //按商家名来排序

            for($i = 0 ;$i<count($arr);$i++){

                $bname = $arr[$i];
                $commodityId = $carts->selCartComid($bname,$uId);
                    //查询出商品ID后，
                $comarr = array();
                //查询商品信息

                foreach ($commodityId as $keya => $valuea){

                    if ($commodity-> selCom('id',$valuea['commodityId'],'id,businessId,commodityName,commodityImg,presentPrice')){
                        $comtext = json_decode($commodity-> selCom('id',$valuea['commodityId'],'id,businessId,commodityName,commodityImg,presentPrice'),true) ;
                    }// TODO $comtext必定有数据，必定有下标0？
                    if(count($comtext) == 0){
                        return responseJson(Base::OK,'购物车内无商品');
                    }
                    $comarr[$keya]= $comtext[0];
                    $comarr[$keya]['businessname']= $bname;
                    //添加最终数组

                }
                $array[$i] = $comarr;

            }
            return responseJson(Base::OK,'查询成功',$array) ;
        }catch (Exception $e){
            throw $e;
        }
    
    }

    public  function  delCart(Request $request,Business $business,Carts $carts){
        try {
            // TODO 将获取缓存信息放在中间件里
            $arr = $request->cacheInfo;
            $commodityId = $request->post('commodityId');
            if (!$data = $carts->cartsdel($arr['id'],$commodityId)){
                return responseJson(Base::FAIL,'数据删除失败');
            }
            return responseJson(Base::OK,'数据删除成功',$data);
        }catch (Exception $e) {
            throw $e;
        }
    }
    
    /**
     * @param Carts $carts
     * @param Commodity $commodity
     * @param Coupon $coupon
     * @param CouponReceive $couponReceive
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    //查询购物车优惠卷（成功）
    public function  selCartcoupon(Request $request,Carts $carts,Commodity $commodity,Coupon $coupon,CouponReceive $couponReceive){
        // TODO 将获取缓存信息放在中间件里，请加上try catch
        $arr = $request->cacheInfo;
        $userId = $arr['id'];//获取用户id

        $businessName = $carts->selCartName($userId);
        try{
            //遍历多维数组
            $arraya = array();
            $arr = array();
            foreach($businessName as $key => $value){
                array_push($arraya,$businessName[$key]['businessName']);
            }
            //清除重复商家名
            $arraya = array_unique($arraya);
            foreach ($arraya as $k=>$v){
                array_push($arr ,$v);
            }

            $array  =array();
            //按商家名来排序

            for($i = 0 ;$i<count($arr);$i++) {
                $bname = $arr[$i];
                $bId = json_decode($carts->selCartBid($bname, $userId),true);
                $coupontext = json_decode($coupon->selCoupon($bId[0]['businessId']),true);
                $couarr = array();
                // 查询优惠卷
                // TODO 代码缩进不规范
                foreach ($coupontext as $key => $value){
                    $couid = $value['id'];
                    $couname = $value['name'];
                    $coutext = $couponReceive->selCoupon($userId,$couid);
                    if(count($coutext)!=0){
                        array_push($couarr,$coutext[0]);
                        for($q=0;$q<count($couarr);$q++){
                            $couarr[$q]['name'] = $couname;
                        }
                    }
                }
                $array[$bname] = $couarr;
            }
            return responseJson(Base::OK,'查询成功',$array);
        }catch (Exception $e){
            throw $e;
        }
     
    }
}
